<%--                                                                       --%>
<%-- Copyright (c) 2001-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ taglib uri="/WEB-INF/struts-html-el.tld" prefix="html-el" %>
<%@ taglib uri="/WEB-INF/struts-bean-el.tld" prefix="bean-el" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<%@ page import =  "com.broadvision.web.util.ViewDynaBean,
		    org.apache.struts.validator.DynaValidatorForm,
                    org.apache.commons.beanutils.PropertyUtils,
                    org.apache.struts.action.DynaActionForm,
                    java.util.List"
%>

<script language="JavaScript">

//
//
//
function closeWindow() {
  self.close();
}

//
// A user has been selected. Pass the userAlias value back to
// the parent window's callback function 'setUserAliasNew()'. And then
// close the popup window.
//
function setUserAliasNew(theForm, index) {
  var parentFormName = theForm.parentFormName.value;
  var parentFieldName = theForm.parentFieldName.value;
  var fN = "theForm.elements['oneResult[" + index + "].firstName'].value";
  var firstName = eval(fN);  
  var lN = "theForm.elements['oneResult[" + index + "].lastName'].value";
  var lastName = eval(lN);
  var fullN = "theForm.elements['oneResult[" + index + "].fullName'].value";
  var fullName = eval(fullN);
  var uA = "theForm.elements['oneResult[" + index + "].userAlias'].value";
  var userAlias = eval(uA);

  // we are merging the original userFind.jsp with this file
  // the userFind.jsp only returns the user alias
  // to differentiate the two, we use the following test:
  if ((null != parentFieldName) &&
      ('from' != parentFieldName) &&
      ('to' != parentFieldName)) {
    window.opener.setUserAlias(parentFormName, parentFieldName, userAlias);
  }
  else {
    window.opener.setUserAliasNew(parentFormName, parentFieldName, firstName, lastName, fullName, userAlias);
  }
  window.close();
}

//
//
//
function selectUserAlias(theForm, index) {
  var uA = "theForm.elements['oneResult[" + index + "].userAlias'].value";
  var userAlias = eval(uA);
  theForm.selectedAlias.value = userAlias;
  theForm.submit();
}

//
//
//
function removeUserAlias(theForm, index) {
  var uA = "theForm.elements['oneResultForRemove[" + index + "].userAlias'].value";
  var userAlias = eval(uA);
  theForm.removeAlias.value = userAlias;
  theForm.submit();
}

//
//
//
function setSelectedUsers(theForm) {
  var parentFormName = theForm.parentFormName.value;
  var parentFieldName = theForm.parentFieldName.value;
  var users = new Array();
  var selected = theForm.selected.value;

  if (selected.length > 0) {
    users = selected.split("$#@!");
  }

  window.opener.setSelectedUsers(parentFormName, parentFieldName, users);
  window.close();
}

  function doSearch(theForm) {
    if (theForm != null) {
      theForm.currentPageNumber.value = 0;
    }
    return true;
  }
  
  function gotoFirstPage(formName) {
    var thisForm = document.forms[formName];
    if (thisForm.maxNoToFind.value <= 0) {
      alert('<bean-el:message key="error.search.maxFound" bundle="mt.messages"/>');
      return false;
    }

    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    if (thisForm != null) {
      thisForm.currentPageNumber.value = 1;
      thisForm.executeSearch.value = "true";
      thisForm.submit();
      return false;
    }
  }
  
  function gotoPreviousPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber--;
      if (thisForm.entriesPerPage.value != <c:out value="${mtUserSearchPopupForm.map.entriesPerPage}"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber > 0) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }
  
  function gotoNextPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      var currentPageNumber = thisForm.currentPageNumber.value;
      currentPageNumber++;
      if (thisForm.entriesPerPage.value != <c:out value="${mtUserSearchPopupForm.map.entriesPerPage}"/>) {
        currentPageNumber = 1;
      }
      if (currentPageNumber <= thisForm.totalPageNumber.value) {
        thisForm.currentPageNumber.value = currentPageNumber;
        thisForm.submit();
      }
    }
  }

  function gotoLastPage(formName) {
    if (doValidateEntriesPerPage(formName) == false) {
      return false;
    }
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage.value != <c:out value="${mtUserSearchPopupForm.map.entriesPerPage}"/>) {
        thisForm.currentPageNumber.value = 1;
      }
      else {
        thisForm.currentPageNumber.value = thisForm.totalPageNumber.value;
      }
      thisForm.submit();
    }
  }


  function doGo(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.maxNoToFind.value <= 0) {
        alert('<bean-el:message key="error.search.maxFound" bundle="mt.messages"/>');
        return false;
      }

      if (thisForm.entriesPerPage.value != <c:out value="${mtUserSearchPopupForm.map.entriesPerPage}"/>) {
        thisForm.currentPageNumber.value = 1;
        thisForm.submit();
        return true;
      }
    }
    return false;
  }
  
  function checkNumberOnly(e) {
    var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
      return false;
    }
    else {
      return true;
    }
  }

  function doValidateEntriesPerPage(formName) {
    var thisForm = document.forms[formName];
    if (thisForm != null) {
      if (thisForm.entriesPerPage == null) {
        return true;
      }
      var entriesPerPageInt = parseInt(thisForm.entriesPerPage.value);
      if (isNaN(entriesPerPageInt)) {
        alert("<bean-el:message key="mt.common.pagination.validation.itemsPerPage" bundle="mt.messages"/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      if (thisForm.entriesPerPage.value < 1 || thisForm.entriesPerPage.value > 2147483647) {
        alert("<bean-el:message key="mt.common.pagination.validation.itemsPerPage" bundle="mt.messages"/>");
        thisForm.entriesPerPage.select();
        thisForm.entriesPerPage.focus();
        return false;
      }
      else {
        return true;
      }
    }
    return false;
  }  

function checkNumberOnly(e) {
  var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
  if (charCode > 31 && (charCode < 48 || charCode > 57)) {
    return false;
  }
  else {
    return true;
  }
}

</script>

<c:set var="multiselect" scope="page" value="${mtUserSearchPopupView.map.multiselect}"/>

<%
  String actionPath = "/mt/userSearchPopup.do";
  Boolean multiselect = (Boolean)pageContext.getAttribute("multiselect");
  if (((Boolean) multiselect).booleanValue()) {
    actionPath = "/mt/userMultiSearchPopup.do";
  }
%>

<c:set var="addImg">
<img src="<%= request.getContextPath() %>/ep/images/action/icon_add.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.add" bundle="mt.messages"/>" title="<bean-el:message key="image.alt.add" bundle="mt.messages"/>">
</c:set>

<c:set var="selectItemImg">
<img src="<%= request.getContextPath() %>/ep/images/action/icon_add.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.selectitem" bundle="mt.messages"/>" title="<bean-el:message key="image.alt.selectitem" bundle="mt.messages"/>">
</c:set>

<c:set var="removeImg">
<img src="<%= request.getContextPath() %>/ep/images/action/icon_remove.gif" width="15" height="15" border="0" class="actionIcon" alt="<bean-el:message key="image.alt.remove" bundle="mt.messages"/>" title="<bean-el:message key="image.alt.remove" bundle="mt.messages"/>">
</c:set>


<html-el:form action="<%= actionPath %>"  onsubmit="return doGo('mtUserSearchPopupForm');">

<html-el:hidden property="parentFormName"/>
<html-el:hidden property="parentFieldName"/>
<html-el:hidden property="selected"/>
<html-el:hidden property="suggested"/>
<html-el:hidden property="selectedAlias"/>
<html-el:hidden property="removeAlias"/>
<html-el:hidden property="currentPageNumber"/>
<html-el:hidden property="totalPageNumber"/>

<!-- Zone 5 starts here.  -->
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0" summary="content area">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <c:choose>
          <c:when test="${mtUserSearchPopupView.map.multiselect}">
            <bean-el:message key="mt.user.popup.multiple.pageTitle" bundle="mt.messages"/>
          </c:when>
          <c:otherwise>
            <bean-el:message key="mt.user.popup.pageTitle" bundle="mt.messages"/>
          </c:otherwise>
        </c:choose>
      </div>
      <div class="generalWrapRight">
        <html-el:button property="closeWinBtn" styleClass="inputButton" onclick="javascript:closeWindow()">
          <bean-el:message key="button.close"  bundle="mt.messages"/>
        </html-el:button>
      </div> 

      <table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="2" summary="User search criteria table">
              <tr>
                <td width="10%" nowrap>
                  <b><bean-el:message key="mt.user.search.label.searchString" bundle="mt.messages"/>:</b><br>&nbsp;
                </td>
                <td colspan=3 nowrap>
                  <html-el:radio name="mtUserSearchPopupForm" property="searchStringAnywhere" value="false"/>
                  <bean-el:message key="mt.user.search.searchBegin" bundle="mt.messages"/>
                  <br>
                  <html-el:radio name="mtUserSearchPopupForm" property="searchStringAnywhere" value="true"/>
                  <bean-el:message key="mt.user.search.searchAnywhere" bundle="mt.messages"/>
                </td>
              </tr>
              <tr> 
                <td width="10%" nowrap><b><bean-el:message key="mt.user.label.searchUser" bundle="mt.messages"/>:</b><br>&nbsp;</td>
                <c:choose>
                  <c:when test="${mtUserSearchPopupView.map.useFullName}">
                    <td width="20%" nowrap>
                      <html-el:text  name="mtUserSearchPopupForm" property="fullName" size="20" maxlength="80" />
                      <br><i>(<bean-el:message key="mt.user.label.fullName" bundle="mt.messages"/>)</i>
                    </td>
                  </c:when>
                  <c:otherwise>
                    <td width="10%" nowrap>
                      <html-el:text name="mtUserSearchPopupForm" property="firstName" size="20" maxlength="80" />
                      <br><i>(<bean-el:message key="mt.user.label.firstName" bundle="mt.messages"/>)</i>
                    </td>
                    <td width="10%" nowrap>
                      <html-el:text name="mtUserSearchPopupForm" property="lastName" size="20" maxlength="80" />
                      <br><i>(<bean-el:message key="mt.user.label.lastName" bundle="mt.messages"/>)</i>
                    </td>
                  </c:otherwise>
                </c:choose>

                <td width="10%" nowrap>
                  <html-el:text  name="mtUserSearchPopupForm" property="userAlias" size="20" maxlength="80"/>
		  <br><i>(<bean-el:message key="mt.user.label.loginId" bundle="mt.messages"/>)</i>
                </td>
                <td width="60%" nowrap align="left">
                  <input hspace="40" type="submit" name="btn_action" class="inputButton" value="<bean-el:message key="button.goto" bundle="mt.messages"/>" onclick="javascript: return gotoFirstPage('mtUserSearchPopupForm');">
                  <br>&nbsp;
                </td>
              </tr>
              <tr> 
                <td width="10%" nowrap>
                  <b><bean-el:message key="mt.user.search.label.maxNumberToFind" bundle="mt.messages"/>:</b><br>&nbsp;
                </td>
                <td width="20%" nowrap>
                  <html-el:text  name="mtUserSearchPopupForm" property="maxNoToFind" size="5" maxlength="80" onkeypress="return checkNumberOnly(event)"/>
                </td>
                <td width="10%" nowrap>
                </td>
                <td width="60%" nowrap>
                </td>
              </tr>
     </table>

<c:if test="${mtUserSearchPopupForm.map.executeSearch}">
            <!-- Start Result List here -->
            <%
              ViewDynaBean mtUserSearchPopupView = (ViewDynaBean) request.getAttribute("mtUserSearchPopupView");
              Integer userListLength = (Integer) PropertyUtils.getSimpleProperty(mtUserSearchPopupView, "numOfUsers");
            %>

            <div class="pageSubtitle">
              <span class="left"><bean-el:message key="mt.user.searchResult" arg0="<%= userListLength.toString() %>"  bundle="mt.messages"/></span>
              <span class="right">&nbsp;</span>
            </div> 

            <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
              <tr class="listHeader">
                <th id="Name" align="left"><bean-el:message key="mt.user.search.header.name" bundle="mt.messages"/></th>
                <th id="Id" align="left"><bean-el:message key="mt.user.search.header.loginId" bundle="mt.messages"/></th>
                <th id="Actions" align="left" nowrap><bean-el:message key="mt.user.search.header.actions" bundle="mt.messages"/></th>
              </tr>
            <c:choose>
              <c:when test="${empty mtUserSearchPopupView.map.searchResultList}">
                <tr class="listEven">
                  <td colspan=3 align="center"><bean-el:message key="mt.user.search.noUser" bundle="mt.messages"/></td>
                </tr>
              </c:when>
              <c:otherwise>
                <c:forEach var="oneResult" items="${mtUserSearchPopupView.map.searchResultList}" varStatus="status">
                  <c:choose>
                    <c:when test="${(status.count + 1) % 2 == 0}">
	              <tr class="listEven">
                    </c:when> 
  	            <c:otherwise>
	              <tr class="listOdd">
                    </c:otherwise>
                  </c:choose>

                    <td headers="Name">
                      <c:out value="${oneResult.map.name}" />
                    </td>
                    <td headers="Id">
                      <c:out value="${oneResult.map.userAlias}"/>&nbsp;
                    </td>
                    <td nowrap align="left" headers="Actions">

<html-el:hidden name="oneResult" property="userAlias" indexed="true"/>

                    <c:choose>
                      <c:when test="${mtUserSearchPopupView.map.multiselect}">
                        <a href="#" onclick="return selectUserAlias(document.forms[0], '<c:out value="${status.count - 1}"/>');"><c:out value='${addImg}' escapeXml="false"/></a>
                      </c:when>
                      <c:otherwise>

<html-el:hidden name="oneResult" property="firstName" indexed="true"/>
<html-el:hidden name="oneResult" property="lastName" indexed="true"/>
<html-el:hidden name="oneResult" property="fullName" indexed="true"/>

                        <a href="#" onclick="return setUserAliasNew(document.forms[0], '<c:out value="${status.count - 1}"/>');"><c:out value='${selectItemImg}' escapeXml="false"/></a>

                      </c:otherwise>
                    </c:choose>
                    </td>
                  </tr>
                </c:forEach>
              </c:otherwise>
            </c:choose>
            </table>
            <!--end result list here-->

            <c:if test="${mtUserSearchPopupView.map.numOfUsers > 0}" >

              <div class="pagination">

                <html-el:select property="entriesPerPage" size="1"  onchange="javascript:doGo('mtUserSearchPopupForm')">
                  <html-el:option value="10">10</html-el:option>
                  <html-el:option value="20">20</html-el:option>
                  <html-el:option value="30">30</html-el:option>
                  <html-el:option value="50">50</html-el:option>
                  <html-el:option value="100">100</html-el:option>
                </html-el:select>
                <bean-el:message key="page.itemsPerPage" bundle="mt.messages"/>
                &nbsp;&nbsp;

                <c:if test="${mtUserSearchPopupForm.map.totalPageNumber > 1}">
                  &nbsp;&nbsp;&nbsp;
                  <c:if test="${mtUserSearchPopupForm.map.currentPageNumber > 0}">
                    <c:if test="${mtUserSearchPopupForm.map.totalPageNumber > 1}">
                      <c:if test="${mtUserSearchPopupForm.map.currentPageNumber > 1}">

                        <a href="#" class="paginationBtn" onclick="javascript:gotoFirstPage('mtUserSearchPopupForm')">
                          <bean-el:message key="page.first" bundle="mt.messages"/></a>
                        <a href="#" class="paginationBtn" onclick="javascript:gotoPreviousPage('mtUserSearchPopupForm')">
                          <bean-el:message key="page.previous" bundle="mt.messages"/></a>
                      </c:if>
                    </c:if>

                    <c:set var="currentPageNumberAttr" scope="page" value="${mtUserSearchPopupForm.map.currentPageNumber}"/>
                    <c:set var="totalPageNumberAttr" scope="page" value="${mtUserSearchPopupForm.map.totalPageNumber}"/>
      
                    <% Integer currentPageNumberInt = (Integer)pageContext.getAttribute("currentPageNumberAttr");
                       Integer totalPageNumberInt = (Integer)pageContext.getAttribute("totalPageNumberAttr"); %>

                    <bean-el:message key="page.pageOf" 
                      arg0="<%= currentPageNumberInt.toString() %>" 
                      arg1="<%= totalPageNumberInt.toString() %>"  bundle="mt.messages"/>

                    <c:if test="${mtUserSearchPopupForm.map.totalPageNumber > 1}">

                      <% if (currentPageNumberInt.intValue() < totalPageNumberInt.intValue()) { %>
                        <a href="#" class="paginationBtn" onclick="javascript:gotoNextPage('mtUserSearchPopupForm')">
                          <bean-el:message key="page.next"  bundle="mt.messages"/></a>
                        <a href="#" class="paginationBtn" onclick="javascript:gotoLastPage('mtUserSearchPopupForm')">
                          <bean-el:message key="page.last"  bundle="mt.messages"/></a>
                      <% } %>
                    </c:if>
                  </c:if>
                </c:if>
              </div>
            </c:if>
</c:if>
<!-- for executeSearch -->
          </td>
        </tr>

<c:if test="${mtUserSearchPopupView.map.multiselect}">
        <tr>
          <td>
            <div class="pageSubtitle">
              <span class="left"><bean-el:message key="mt.user.search.selectedUsersSubtitle"  bundle="mt.messages"/></span>
              <span class="right">&nbsp;</span>
            </div>

            <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Search result table" width="100%">
              <tr class="listHeader">
                <th id="Name" align="left"><bean-el:message key="mt.user.search.header.name" bundle="mt.messages"/></th>
                <th id="Id" align="left"><bean-el:message key="mt.user.search.header.loginId" bundle="mt.messages"/></th>
                <th id="Actions" align="left" nowrap><bean-el:message key="mt.user.search.header.actions" bundle="mt.messages"/></th>
              </tr>
              <c:choose>
                <c:when test="${empty mtSelectedUserView.map.selectedList}">
                  <tr class="listEven">
                    <td colspan=3 align="center"><bean-el:message key="mt.user.search.noSelectedUsers" bundle="mt.messages"/></td>
                  </tr>
                </c:when>
     
                <c:otherwise>
                  <c:forEach var="oneResultForRemove" items="${mtSelectedUserView.map.selectedList}" varStatus="status">
                    <c:choose>
                      <c:when test="${(status.count + 1) % 2 == 0}">
	                <tr class="listEven">
                      </c:when> 
  	              <c:otherwise>
	                <tr class="listOdd">
                      </c:otherwise>
                    </c:choose>

                      <td headers="Name">
                        <c:out value="${oneResultForRemove.map.name}" />
                      </td>
                      <td headers="Id">
                        <c:out value="${oneResultForRemove.map.userAlias}"/>&nbsp;
                      </td>
                      <td nowrap align="left" headers="Actions">

<html-el:hidden name="oneResultForRemove" property="userAlias" indexed="true"/>

                        <a href="#" onclick="return removeUserAlias(document.forms[0], '<c:out value="${status.count - 1}"/>');"><c:out value='${removeImg}' escapeXml="false"/></a>
                      </td>
                    </tr>
                  </c:forEach>
                </c:otherwise>
              </c:choose>
            </table>

            <div class="pageButtonWrap" align="right">
              <input hspace="40" type="button" name="btn_action" class="pageButton" value='<bean-el:message key="button.done" bundle="mt.messages"/>' onclick="JavaScript:setSelectedUsers(document.forms[0])">
            </div>

          </td>
        </tr>
</c:if>

</table>

<!--end zone 5 content here -->

  <c:choose>
        <c:when test="${empty mtUserSearchPopupForm.map.suggested}">
          <input type="hidden" name="executeSearch" value="true"/>
        </c:when>
        <c:otherwise>
          <input type="hidden" name="executeSearch" value="false"/>
        </c:otherwise>
  </c:choose>   

</html-el:form>

